package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created by cc
 * Description:
 * User: CZH
 * Date: 2022-09-23
 * Time: 21:23
 */
public class DelOperation implements IOPeration {
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书！");
        System.out.println("请输入您要删除的图书名字：");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int size = bookList.getUsedSize();
        int pos = -1;
        for (int i = 0; i < size; i++){
            Book book = bookList.getPos(i);
            if (book.getName().equals(name)) {
                pos = i;
                break;
            }
        }
        if (pos != -1) {
            for (int i = pos; i < size-1; i++){
                Book book = bookList.getPos(i+1);
                bookList.setBooks(i,book);
            }
            bookList.setUsedSize(size-1);
            System.out.println("删除成功！");
            return;
        }
        System.out.println("删除失败！");
    }
}
